package gcb.metalsys.controller.cop;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import gcb.metalsys.base.ResultUtil;
import gcb.metalsys.base.dto.ResultDTO;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import gcb.metalsys.service.cop.CopClientService;
import gcb.metalsys.entity.cop.CopClient;

/**
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/copClient")
public class CopClientController {

    @Autowired
    private CopClientService copClientService;

    @PostMapping
    @ApiOperation(value = "插入")
    public ResultDTO insertOne(@RequestBody CopClient copClient) throws Exception {
        copClientService.save(copClient);
        return ResultUtil.Success(copClient);
    }

    @PutMapping
    @ApiOperation(value = "按ID修改")
    public ResultDTO updateById(@RequestBody CopClient copClient) throws Exception {
        copClientService.updateById(copClient);
        return ResultUtil.Success();
    }

    @DeleteMapping("/{id:\\d+}")
    @ApiOperation("按ID删除")
    public ResultDTO deleteById(@PathVariable Long id) throws Exception {
        copClientService.removeById((long) id);
        return ResultUtil.Success();
    }

    @GetMapping()
    @ApiOperation("查询、分页返回")
    public ResultDTO selectEntityPage(String clientName,
                                      @RequestParam(value = "page", defaultValue = "0") int pageNum,
                                            @RequestParam(value = "limit", defaultValue = "8") int pageSize)throws Exception{
        QueryWrapper<CopClient> queryWrapper = new QueryWrapper<>();
        if (clientName != null && !clientName.equals("")) {
           queryWrapper.like("client_name",clientName);
        }
        IPage<CopClient> page = copClientService.page(new Page<CopClient>(pageNum, pageSize),queryWrapper);
        List<CopClient> data = page.getRecords();

        for (CopClient client : data) {
            LocalDateTime today = LocalDateTime.now();
            int todayYear = Integer.parseInt(today.format(DateTimeFormatter.ofPattern("yyyy")));
            int todayMonth = Integer.parseInt(today.format(DateTimeFormatter.ofPattern("MM")));

            LocalDateTime clientBirthday = client.getClientBirthday();

            int birthdayYear = Integer.parseInt(clientBirthday.format(DateTimeFormatter.ofPattern("yyyy")));
            int birthdayMonth = Integer.parseInt(clientBirthday.format(DateTimeFormatter.ofPattern("MM")));

            int age = todayYear - birthdayYear;
            if(todayMonth > birthdayMonth){
                age -= 1;
            }
            client.setClientAge(age);
        }

        int count = (int) page.getTotal();
        return ResultUtil.Success(data,count);
    }

    @GetMapping("/{id:\\d+}")
    @ApiOperation("ID查询")
    public ResultDTO selectEntityPage(@PathVariable Long id)throws Exception{
        return ResultUtil.Success(copClientService.getById(id));
    }

}
